﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Diagnostics;
using System.Runtime.InteropServices; 
using System.Drawing.Drawing2D;


namespace web
{
    public partial class Form1 : Form
    {
        private ArrayList arrayList_link = new ArrayList();
        private ArrayList arrayList_node = new ArrayList();

        private const string EDITOR = "编辑";
        private const string ADD = "增加";
        private const string DELETE = "删除";
        private const string COLUMN = "栏目";
        private const string WEB = "收藏";
        private string PROJECT = "-老狼收藏夹-尘土网版权所有";
        private string NAME = "老狼收藏夹";

        private string userName = "";
        private string userId = "";
        private int column_id = 0;
        private int web_site_id = 0;

        public string type = COLUMN;

        private ArrayList my_color = new ArrayList();
        private ContextMenuStrip menuStrip = new ContextMenuStrip();//显示栏目右键菜单

        public Form1(string user,string userId)
        {
            this.userName = user;
            this.userId = userId;
            if (this.userId.Length < 1 || this.userName.Length < 1)
            {
                MessageBox.Show("发生错误，即将关闭！");
                this.Close();
            }
            InitializeComponent();
            
           // this.treeView_column.ContextMenuStrip=new this.ContextMenu;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Size = new System.Drawing.Size(this.Width, SystemInformation.PrimaryMonitorSize.Height-60);
            this.Form_SizeChange();
            this.Location = new Point(SystemInformation.PrimaryMonitorSize.Width - this.Width, 0);
            this.set_color();
            this.Text = this.userName.ToString() + "收藏夹" + this.PROJECT;
            this.load_column();
            this.load_site();
            this.load_website(this.column_id);
            this.create_set_menuStrip();
            this.notifyIcon1.ContextMenuStrip = this.icon_create_menuStrip();
            this.Show_time();
            this.bian_jian_Load();
        }

        //==============================================================
        //用来生成web_struct数组，
        //==============================================================
        private void load_site()
        {
            try
            {
                string sql = "select menuid,click,name,remark,url,list,userid,id from website where userid='" + this.userId + "'";
                this.mssql();
                SqlCommand query = new SqlCommand(sql, SqlConn.conn);
                SqlDataReader rs = query.ExecuteReader();
                arrayList_link.Clear();

                while (rs.Read())
                {
                    web_struct a;
                    a.menuid = Convert.ToInt16(rs.GetSqlValue(0).ToString().Trim());
                    a.name = rs.GetSqlValue(2).ToString().Trim();
                    a.url = rs.GetSqlValue(4).ToString().Trim();
                    a.remark = rs.GetSqlString(3).ToString().Trim();
                    a.id = Convert.ToInt16(rs.GetSqlValue(7).ToString().Trim());
                    this.arrayList_link.Add(a);
                }
                rs.Close();
                SqlConn.mssql_close();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                this.Close();
            }
        }

        //==============================================================
        //用来设置标题
        //==============================================================
        private void set_title(int i)
        {
            string sql = "select name from web where id='" + i + "'";
            this.mssql();
            SqlCommand query = new SqlCommand(sql, SqlConn.conn);
            SqlDataReader rs = query.ExecuteReader();
            if (rs.Read())
            {
                this.Text = rs.GetString(0);
            }
            else
            {
                this.Text = this.userName.Trim() + "收藏夹"+this.PROJECT;
            }
            rs.Close();
            SqlConn.mssql_close();       
        }

       
        //===============================================================================
        //执行SQL语句，判断影响行数，如果影响行数大于0；则返回false,并弹出对话框
        //===============================================================================
        private bool check_no_sub(string sql)
        {
            SqlCommand query = new SqlCommand(sql, SqlConn.conn);
                bool return_bool = true;
                SqlDataReader rs = query.ExecuteReader();
                if (true==rs.Read())
                {
                    MessageBox.Show("当前栏目下还有子栏目或者收藏，不能删除！");
                    return_bool=false;
                }
                rs.Close();
                return return_bool;
        }

        //===============================================================================
        //显示错误
        //===============================================================================
        private void error_web()
        {
            MessageBox.Show("网络错误，请稍候再试！");
        }


        //===============================================================================
        //显示所有栏目，移动栏目或者收藏时菜单
        //===============================================================================
        private void websiteItem_move_column_Click(ToolStripMenuItem stripItem,int n,EventHandler fun_str)
        {
            if (n == 0)
            {
                ToolStripMenuItem mainItem = new ToolStripMenuItem();
                mainItem.Text = "我的收藏";
                mainItem.Name = "0";
                mainItem.Click += fun_str;
                stripItem.DropDown.Items.Add(mainItem);
            }
            foreach (column_struct c in this.arrayList_node)
            {
                if (c.parentId == n)
                {
                    ToolStripMenuItem temp = new ToolStripMenuItem();
                    temp.Text = c.name;
                    temp.Name = c.id.ToString();
                    temp.Click += fun_str;
                    stripItem.DropDown.Items.Add(temp);

                    this.websiteItem_move_column_Click(temp,c.id, fun_str);

                }
            }
        }
 


        //===============================================================================
        //顶部显示时间
        //===============================================================================
        public void Show_time()
        {
            ShowTime showTime = new ShowTime(this.label_show_time);
            showTime.get_now_time();
        }


        //===============================================================================
        //数据库连接
        //===============================================================================
        private void mssql()
        {
            if (SqlConn.conn == null || SqlConn.conn.State.ToString().ToLower() == "closed")
            {
                SqlConn.mssql_connection();
                if (SqlConn.conn == null)
                {
                    this.error_web();
                }
            }
        }

        private void treeView1_AfterSelect(object sender, EventArgs e)
        {
            this.show_web_site();
        }
       
        //===============================================================================
        //显示收藏
        //===============================================================================
       private void show_web_site()
       {
           if (this.treeView_column.SelectedNode!=null)
           {
               this.column_id=Convert.ToInt16(this.treeView_column.SelectedNode.Name.ToString().Substring(2));
               this.load_website(this.column_id);
           }
        }

        private ContextMenuStrip icon_create_menuStrip()
        {
            ContextMenuStrip menuStrip = new ContextMenuStrip();
            ToolStripMenuItem item_show = new ToolStripMenuItem();
            item_show.Text = "显示" + this.PROJECT;
            item_show.Click += new EventHandler(this_show);

            ToolStripMenuItem item_show_min = new ToolStripMenuItem();
            item_show_min.Text = "最小化" + this.PROJECT;
            item_show_min.Click += new EventHandler(this_show_min_form);

            ToolStripMenuItem item_close = new ToolStripMenuItem();
            item_close.Text = "关闭" + this.PROJECT;
            item_close.Click += new EventHandler(this_close);

            menuStrip.Items.Add(item_show);
            menuStrip.Items.Add(item_show_min);
            menuStrip.Items.Add(item_close);

            return menuStrip;

        }

        //===============================================================================
        //关闭from
        //===============================================================================
        private void this_close(Object sender, EventArgs e)
        {
            this.Close();
        }

        //===============================================================================
        //最小化from
        //===============================================================================
        private void this_show_min_form(Object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
            this.hidden_form();
        }

        //===============================================================================
        //显示from
        //===============================================================================
        private void this_show(Object sender, EventArgs e)
        {
            this.show_form();
            
        }


        //===============================================================================
        //设置在状态栏显示
        //===============================================================================
        private void show_web_site_normal(object sender, EventArgs e)
        {
            this.hidden_form();
        }
        
        //===============================================================================
        //双击还原
        //===============================================================================
        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.show_form();
            }
            else
            {
                this.show_web_site_normal(sender, e);
            }
        }


        //===============================================================================
        //显示form
        //===============================================================================
        private void show_form()
        {
            
            this.Focus();
            this.notifyIcon1.Text = this.PROJECT.Substring(1);
            this.Width = 337;
            this.Show();
            this.WindowState = FormWindowState.Normal;
            this.Form_SizeChange();
            
        }

        //===============================================================================
        //隐藏form
        //===============================================================================
        private void hidden_form()
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Hide();
                this.notifyIcon1.Text = "双击还原" + this.PROJECT;
            }
        }


        //选定内容更更后显示
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            this.select_node();
        }


  
        ~Form1()
        {
            SqlConn.mssql_close();
        }

        private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            
            //this.Location=new System.Drawing.Point(this.win
            this.Form_SizeChange();
        }

        private void Form_SizeChange()
        {
            int bianjian = 110;
            int _max = this.Height;
            this.panel1.Height = _max - 47;
            this.splitContainer1.Height = this.panel1.Height - 44 - bianjian;
            this.treeView_column.Height = this.splitContainer1.Height-5;
            if (this.Height > 600)
            {
                this.treeView_column.Font=new Font("宋体",11F);
            }
        }

      

        private void richText_bianJian_load(string type)
        {
           // ArrayList temp=
        }







    }
}



//==========================================================================
//重画系统状态栏
//==========================================================================
/* private void timer1_Tick(object sender, EventArgs e)
 {
    // WndProc(m_rect)
     //WndProc(ref Message m);
 }

 [DllImport("User32.dll ")]

 private static extern IntPtr GetWindowDC(IntPtr hwnd);

 [DllImport("User32.dll ")]

 private static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc);

 [DllImport("Kernel32.dll ")]

 private static extern int GetLastError();

 Rectangle m_rect = new Rectangle(165, 0, 180, 30); 
 protected override void WndProc(ref Message m)
 {

     base.WndProc(ref m);

     switch (m.Msg)
     {

         case 0x86://WM_NCACTIVATE 

             goto case 0x85;

         case 0x85://WM_NCPAINT 
             {

                 IntPtr hDC = GetWindowDC(m.HWnd);

                 //把DC转换为.NET的Graphics就可以很方便地使用Framework提供的绘图功能了 

                 this.gs = Graphics.FromHdc(hDC);

                // gs.FillRectangle(new LinearGradientBrush(m_rect, Color.FromArgb(99, 138, 195), Color.FromArgb(146, 191, 230), LinearGradientMode.BackwardDiagonal), m_rect);

                 StringFormat strFmt = new StringFormat();

                 strFmt.Alignment = StringAlignment.Center;

                 strFmt.LineAlignment = StringAlignment.Center;

                 this.gs.DrawString(this.Show_time(), this.Font, Brushes.BlanchedAlmond, m_rect, strFmt);

                 this.gs.Dispose();

                 //释放GDI资源 

                 ReleaseDC(m.HWnd, hDC);

                 break;

             }

         case 0xA1://WM_NCLBUTTONDOWN 
             {

                 Point mousePoint = new Point((int)m.LParam);

                 mousePoint.Offset(-this.Left, -this.Top);

                 break;

             }

     }

     WndProc(ref m);
 } 


 */